<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2024/3/5
 * Time: 17:27
 */

namespace app\api\controller;

use app\api\model\Xun AS XunModel;

class Xun extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 提交询价单
     * username    联系人
     * phone       联系方式
     * desc        描述
     * @return \think\response\Json
     */
    public function commitXunOrder(){
        $xun_username = input('username');
        $xun_phone = input('phone');
        $xun_desc = input('desc');

        if(!$xun_username) return json(['status' => 0, 'msg' => $this->lanMsg->requireConcatUser[$this->lanType]]);
        if(!$xun_phone) return json(['status' => 0, 'msg' => $this->lanMsg->requireConcatPhone[$this->lanType]]);
        if(!$xun_desc) return  json(['status' => 0, 'msg' => $this->lanMsg->requireDesc[$this->lanType]]);

        $insert = [
            'xun_user_id' => $this->user_id,
            'xun_username' => $xun_username,
            'xun_phone' => $xun_phone,
            'xun_desc' => $xun_desc,
            'xun_time' => initTime()
        ];

        $res = XunModel::insertData($insert);

        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 询价单列表
     * page    页码
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function xunOrderList(){
        $page = input('page') ? input('page') : 1;

        $where = ['xun_user_id' => $this->user_id];
        $data = XunModel::selectData($where, $page);
        if($data){
            $count = XunModel::countData($where);
            return json(['status' => 200, 'msg' => $data, 'count' => $count]);
        }else{
            return json(['status' => 200, 'msg' => [], 'count' => 0]);
        }
    }

    /**
     * 详情
     * xun_id    询价单ID
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function showXunOrder(){
        $xun_id = input('xun_id');

        $data = XunModel::findData(['xun_id' => $xun_id]);
        return json(['status' => 200, 'msg' => $data]);
    }

    /**
     * 已读
     * xun_id    询价单ID
     * @return \think\response\Json
     */
    public function alreadyRead(){
        $xun_id = input('xun_id');

        $xun = XunModel::findData(['xun_id' => $xun_id]);
        if($xun['xun_isRead'] == 1){
            $update = [
                'xun_isRead' => 2,
            ];
            $res = XunModel::update($update, ['xun_id' => $xun_id]);
        }else{
            $res = true;
        }
        if($res){
            return json(['status' => 200, 'msg' => $this->lanMsg->doSuccess[$this->lanType]]);
        }else{
            return json(['status' => 0, 'msg' => $this->lanMsg->systemErr[$this->lanType]]);
        }
    }

    /**
     * 未读记录数量
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function alreadyReadNumber(){
        $where = [
            'xun_user_id' => $this->user_id,
            'xun_isRead' => 1
        ];
        $count = XunModel::countData($where);
        return json(['status' => 200, 'msg' => $count]);
    }
}